/**
 * 数据源列表初始化
 */
var SdsSource = {
    id: "sourceTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    sourceId: 0,
    sourceDesc:null
};

SdsSource.onClickSource = function (e, treeId, treeNode) {
   /* if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体数据源。");
        return false;
    }*/
    SdsSource.sourceId = treeNode.id;
    SdsSource.sourceDesc = treeNode.name;
    //查询数据源输出内容
    SdsStandSourceField.seItem = null;
    SdsStandSourceField.search();
};

/**
 * 数据源输出内容管理初始化
 */
var SdsStandSourceField = {
    id: "SdsStandSourceFieldTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandSourceField.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '字典来源', field: 'sourceDesc', align: 'center', valign: 'middle', sortable: true,  visible: false},
        {title: '字段代码', field: 'fieldCode', align: 'center', valign: 'middle', sortable: true},
        {title: '字段名称', field: 'fieldDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '备注说明', field: 'resume', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:SdsStandSourceField.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:SdsStandSourceField.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
SdsStandSourceField.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsStandSourceField.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增数据源输出内容
 */
SdsStandSourceField.add = function () {
    if (SdsSource.sourceId) {
        var index = Sys.open('新增数据源输出内容', Sys.backendPath + '/sds/sdsstandsourcefield/to_add', ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#sourceId").attr("value", SdsSource.sourceId);
            $(iframeWin.document).find("#sourceDesc").attr("value", SdsSource.sourceDesc);
        });

    } else {
        Sys.info("请先选中某一数据源！");
        return false;
    }
    this.layerIndex = index;
};

/**
 * 打开查看数据源输出内容详情
 */
SdsStandSourceField.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('数据源输出内容详情',Sys.backendPath + '/sds/sdsstandsourcefield/to_update/' + id + "/",['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#sourceDesc").attr("value",SdsSource.sourceDesc);
        });
        this.layerIndex = index;
    }
};

/**
 * 删除数据源输出内容
 */
SdsStandSourceField.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandsourcefield/delete", function (data) {
	            Sys.success("删除成功!");
	            SdsStandSourceField.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsStandSourceFieldId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除数据源输出内容?",operation);
    }
};

SdsStandSourceField.exp = function () {
    if (SdsSource.sourceId) {
        var operation = function(){
            Sys.download(Sys.backendPath + "/sds/sdsstandsourcefield/export",{sourceId:SdsSource.sourceId});
        };
        Sys.confirm("是否导出到excel",operation);
    } else {
        Sys.info("请先选中某一病种！");
    }
};

SdsStandSourceField.imp = function () {
    var index = layer.open({
        type: 2,
        title: '导入数据源输出内容',
        area: ['550px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/sds/sdsstandsourcefield/to_import'
    });
    this.layerIndex = index;
};

/**
 * 查询数据源输出内容列表
 */
SdsStandSourceField.search = function () {
    var queryData = {};
    queryData['fieldCode'] = $("#fieldCode").val();
    queryData['fieldDesc'] = $("#fieldDesc").val();
    queryData['isActive'] = $("#isActive").val();
    queryData['resume'] = $("#resume").val();
    queryData['sourceId'] =  SdsSource.sourceId;
    SdsStandSourceField.table.refresh({query: queryData, pageNumber: 1});
};

$(function () {
    //查询数据源树
    var ztree = new $ZTree("sourceTree", Sys.backendPath + "/sds/sdsstandsource/tree");
    ztree.bindOnClick(SdsSource.onClickSource);
    ztree.init();
    SdsSource.ztree = ztree;
    fuzzySearch('sourceTree','#seachKey',null,true);


    var defaultColunms = SdsStandSourceField.initColumn();
    var table = new BSTable(SdsStandSourceField.id, Sys.backendPath + "/sds/sdsstandsourcefield/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(15);
    table.setSubHeight(-10);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setQueryParams({"sourceId":0})
    SdsStandSourceField.table = table.init();
});
